package com.phiyon.mytry.phone;

import java.util.ArrayList;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.telephony.SmsManager;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

	EditText edtPhoneNumber = null;
	EditText edtSmsContent = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Button button = (Button)this.findViewById(R.id.btn_call);
		button.setOnClickListener(new ButtonClickListener());
		
		edtPhoneNumber = (EditText)this.findViewById(R.id.editText1);
		edtSmsContent  = (EditText)this.findViewById(R.id.editSms);
		
		Button btnSms = (Button)this.findViewById(R.id.btn_sms);
		btnSms.setOnClickListener(new BtnSmsClickListener());
	}

	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	private final class ButtonClickListener implements View.OnClickListener{

		@Override
		public void onClick(View v) {
			String phoneNumber = edtPhoneNumber.getText().toString();
			Intent intent = new Intent();
			intent.setAction("android.intent.action.CALL");
			intent.addCategory("android.intent.category.DEFAULT");
			intent.setData(Uri.parse("tel:" + phoneNumber));  
			startActivity(intent);
		}
		
	}
	

	private final class BtnSmsClickListener implements View.OnClickListener{

		@Override
		public void onClick(View v) {
			String phoneNumber = edtPhoneNumber.getText().toString();
			String smsContent  = edtSmsContent.getText().toString();
			SmsManager manager = SmsManager.getDefault();
			ArrayList<String> texts = manager.divideMessage(smsContent);
			for(String text:texts){
				manager.sendTextMessage(phoneNumber, null, text, null, null);
			}
			
			Toast.makeText(getApplicationContext(), R.string.tip_sms_sucess, Toast.LENGTH_LONG).show();
		}
		
	}
}
